<template>
  <!--群管理-->
  <div class="statistics-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="8">
        <el-button type="primary" @click="add">添加群</el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="序号"
        sortable
        type="index"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="title"
        label="群名"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="user_count"
        label="用户数"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="order"
        label="排序"
      />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="users(scope.row)">用户列表</el-button>
          <el-button type="text" @click="edit(scope)">编辑</el-button>
          <el-button type="text" @click="delList(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 群用户列表 -->
    <el-dialog :title="wintit" :visible.sync="dialogUser">
      <el-table
        v-loading="listLoading"
        :data="userList"
        @selection-change="setSelectRows"
      >
        <el-table-column
          align="center"
          show-overflow-tooltip
          label="序号"
          sortable
          type="index"
        />
        <el-table-column
          align="center"
          show-overflow-tooltip
          prop="appid_info.nickname"
          label="名称"
        />
        <el-table-column
          align="center"
          show-overflow-tooltip
          prop="appid_info.mobile"
          label="手机号"
        />
        <el-table-column align="center" show-overflow-tooltip label="头像">
          <template #default="{ row }">
            <el-image
              style="width: 60px; height: 60px"
              :src="row.appid_info.logo"
              :preview-src-list="[row.appid_info.logo]"
            />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page="queryForm.page"
        :page-size="queryForm.list_row"
        :layout="layout"
        :total="totalOnetag"
        @size-change="winSizeChange"
        @current-change="winCurrentChange"
      />
    </el-dialog>

    <!-- 添加编辑 -->
    <el-dialog :title="wintit" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-width="100px">
        <el-form-item label="标题:">
          <el-input v-model="form.title" placeholder="请输入标题" :rows="15" />
        </el-form-item>
        <el-form-item label="最大人数:">
          <el-input
            v-model="form.user_max"
            placeholder="群用户最大人数"
            :rows="15"
          />
        </el-form-item>
        <el-form-item label="排序值:">
          <el-input
            v-model="form.order"
            placeholder="请输入排序值,越大越在前面"
            :rows="15"
          />
        </el-form-item>
        <el-form-item label="上传群图片:">
          <el-upload
            class="uploads"
            action=""
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :http-request="uploadImg"
          >
            <img v-if="img" width="150px" :src="img" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogOnetag = false">取 消</el-button>
        <el-button v-show="addtype" type="primary" @click="send()">
          确 定
        </el-button>
        <el-button v-show="edittype" type="primary" @click="save()">
          确 定
        </el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
  import {
    getCrowdLists,
    getCrowdUserLists,
    addCrowds,
    editCrowds,
    delCrowds,
    getOssUploadsParam,
  } from '@/api/common'
  import axios from 'axios'

  export default {
    name: 'Recruit',
    data() {
      return {
        user: [],
        queryForm: {
          page: 1,
          list_row: 10,
          keyword: '',
        },
        form: {
          title: '',
          user_max: '',
          order: '',
        },
        disabled: false,
        listLoading: false,
        list: [],
        userList: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        dialogFormVisible: false,
        dialogUser: false,
        wintit: '',
        coId: '',
        img: '',
        addtype: false,
        edittype: false,
        totalOnetag:'',
      }
    },
    created() {
      this.getData()
    },
    methods: {
      handleRemove() {},
      handlePictureCardPreview() {},
      handleDetail() {},
      //搜索
      search() {
        this.getData()
      },
      restart() {
        this.queryForm = []
        this.getData()
      },
      getData() {
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
          keyword: this.queryForm.keyword,
        }
        getCrowdLists(params).then((data) => {
          this.list = data.data
          this.total = data.count
        })
      },
      add() {
        this.dialogFormVisible = true
        this.wintit = '添加'
        this.addtype = true
        this.edittype = false
        this.form = []
        this.img = ''
      },
      edit(row) {
        this.dialogFormVisible = true
        this.wintit = '编辑'
        this.addtype = false
        this.edittype = true
        this.form = this.list[row.$index]
        this.img = this.list[row.$index].url
        this.coId = row.row.id
      },
      send() {
        let params = {
          title: this.form.title,
          order: this.form.order,
          user_max: this.form.user_max,
          url: this.img,
        }
        addCrowds(params).then((data) => {
          this.$message.success('添加成功！')
          this.dialogFormVisible = false
          this.getData()
        })
      },
      save() {
        let params = {
          id: this.coId,
          title: this.form.title,
          order: this.form.order,
          user_max: this.form.user_max,
          url: this.img,
        }
        editCrowds(params).then((data) => {
          this.$message.success('修改成功！')
          this.dialogFormVisible = false
          this.getData()
        })
      },
      uploadImg(params) {
        console.log(123, params)
        const file = params.file,
          fileType = file.type,
          isImage = fileType.indexOf('image') != -1,
          isLt2M = file.size / 1024 / 1024 < 2
        // 这里常规检验，看项目需求而定
        if (!isImage) {
          this.$message.error('只能上传图片格式png、jpg、gif!')
          return
        }
        if (!isLt2M) {
          this.$message.error('只能上传图片大小小于2M')
          return
        }
        getOssUploadsParam().then((res) => {
          const form = new FormData()
          form.append('key', res.data.dir + file.name)
          form.append('OSSAccessKeyId', res.data.accessid)
          form.append('policy', res.data.policy)
          form.append('success_action_status', '200')
          form.append('signature', res.data.signature)
          form.append('file', file)
          console.log(222, res)
          axios({
            url: res.data.host,
            method: 'POST',
            data: form,
            processData: false,
            cache: false,
            contentType: false,
          }).then((data) => {
            this.img = res.data.host + '/' + res.data.dir + file.name
            console.log(66, this.img)
          })
        })
      },
      users(row) {
        this.dialogUser = true
        this.wintit = '查看 ' + row.title + ' 用户数据'
        this.groupid = row.id
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
          id: row.id,
        }
        getCrowdUserLists(params).then((data) => {
          this.userList = data.data
          this.totalOnetag = data.count
        })
      },
      delList(row) {
        let params = {
          id: row.id,
        }
        delCrowds(params).then((data) => {
          this.$message.success('删除成功！')
          this.getData()
        })
      },
      setSelectRows() {},
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.getData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.getData()
      },
      winSizeChange(val) {
        this.queryForm.list_row = val
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
          id: this.groupid,
        }
        getCrowdUserLists(params).then((data) => {
          this.userList = data.data
          this.totalOnetag = data.count
        })
      },
      winCurrentChange(val) {
        this.queryForm.page = val
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
          id: this.groupid,
        }
        getCrowdUserLists(params).then((data) => {
          this.userList = data.data
          this.totalOnetag = data.count
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-button {
    font-size: 14px;
  }
</style>
